/*
 * GoogleCalendarManagerEvoApp.java
 */

package googlecalendarmanagerevo;


import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.util.AuthenticationException;
import it.illumedellaragione.googlecalendar.ManageConfig;
import it.illumedellaragione.googlecalendar.ManageGoogleCalendar;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.lang.String;
import java.net.URL;
import javax.swing.JOptionPane;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
 * The main class of the application.
 */
public class GoogleCalendarManagerEvoApp extends SingleFrameApplication {

    private String username,password;
    private String offsetUTC;
    private boolean autoconnection=false;
    CalendarService myService;
    ManageGoogleCalendar manage;

    private GoogleCalendarManagerEvoLoginDialog loginDialog;

    private String PATH_FILE_CONFIG ="config.properties";


    private void readConfig(){

        username =ManageConfig.getString(PATH_FILE_CONFIG, "user");
        password= ManageConfig.getString(PATH_FILE_CONFIG, "pwd");
        String tAutoconnection = ManageConfig.getString(PATH_FILE_CONFIG, "autoconnection");
        autoconnection = tAutoconnection != null && tAutoconnection.equalsIgnoreCase("true");
        offsetUTC = ManageConfig.getString(PATH_FILE_CONFIG, "offsetUTC");
    }

    private void connect2GoogleServices() throws AuthenticationException {

        manage = new ManageGoogleCalendar();
        myService = manage.retrieveService(username, password);

    }

    /**
     * At startup create and show the main frame of the application.
     */
    @Override protected void startup() {

        // create properties file if not exist        
        try{
            File file = new File(PATH_FILE_CONFIG);
            if (!file.exists()) file.createNewFile();
        }catch(Exception e){e.printStackTrace();}

        readConfig();

        if (!autoconnection){
        loginDialog = new GoogleCalendarManagerEvoLoginDialog(this.getApplication().getMainFrame(),true,username,password);
        show(loginDialog);
        username= loginDialog.getUser();
        password = loginDialog.getPassword();        
        }

        try{
            
        connect2GoogleServices();        
        
        show(new GoogleCalendarManagerEvoView(this,  myService, manage, username,offsetUTC));

        //show(new GoogleCalendarManagerEvoView(this,  username,  password));

        //show(new GoogleCalendarManagerEvoView(this));
        }
        catch(Exception e){
            e.printStackTrace();
            JOptionPane.showMessageDialog(this.getMainFrame(), e.getMessage(),"Error", JOptionPane.ERROR_MESSAGE);
            System.exit(-1);
        }
        
    }

    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override protected void configureWindow(java.awt.Window root) {
    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of GoogleCalendarManagerEvoApp
     */
    public static GoogleCalendarManagerEvoApp getApplication() {
        return Application.getInstance(GoogleCalendarManagerEvoApp.class);
    }

    /**
     * Main method launching the application.
     */
    public static void main(String[] args) {
        launch(GoogleCalendarManagerEvoApp.class, args);
    }

    public void setUser(String s){
        username=s;
    }

    public void setPassword(String s){
        password=s;
    }

}
